/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import core.Settings;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class SystemSettingsForm extends BasicForm
{
    private static final int SAVE_COMMAND_ID = 1;
    
    private TextField preordersUpdatePeriod;
    private TextField maxArchivOrdersCount;
    
    public SystemSettingsForm()
    {
        super("system_settings");
        
        init();
    }
    
    public void show()
    {
        setContent();
 
        //Убираем среднюю кнопку
        if (Display.getInstance().isTouchScreenDevice())
        {
            Display.getInstance().setThirdSoftButton(false); 
        }
        
        super.show();
    }

    public void hideNotify() 
    {
        //Возвращаем среднюю кнопку
        Display.getInstance().setThirdSoftButton(true); 
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);

        //Период автоматического обновления предварительных
        TextArea label = new TextArea((new Label("preord_update_period")).getText(), 1, 30);
        label.setEditable(false);
        label.setFocusable(false);
        label.setGrowByContent(true);
        addComponent(label);
        preordersUpdatePeriod = new TextField(); 
        preordersUpdatePeriod.setSelectCommandText("");
        preordersUpdatePeriod.setInputModeOrder(new String[]{"123"});
        preordersUpdatePeriod.setConstraint(TextArea.NUMERIC);
        addComponent(preordersUpdatePeriod);
        
        //Количество заказов в архиве
        label = new TextArea((new Label("archive_orders_count")).getText(), 1, 30);
        label.setEditable(false);
        label.setFocusable(false);
        label.setGrowByContent(true);
        addComponent(label);
        maxArchivOrdersCount = new TextField(); 
        maxArchivOrdersCount.setSelectCommandText("");
        maxArchivOrdersCount.setInputModeOrder(new String[]{"123"});
        maxArchivOrdersCount.setConstraint(TextArea.NUMERIC);
        addComponent(maxArchivOrdersCount);
        
        addComponent(new Label(" "));
        
        addCommand(new BackCommand());
        
        Command saveCommand = new Command("save", SAVE_COMMAND_ID) 
        {
            public void actionPerformed(ActionEvent evt)
            {                
                save();
                        
                ScreenEngine.getEngine().pop(true);
                
                evt.consume();
            }
        };
        addCommand(saveCommand);
    }
    
    private void save()
    {
        Settings settings = Settings.getSettings();
        
        settings.setPreordersUpdatePeriod(Integer.parseInt(preordersUpdatePeriod.getText()));
        settings.setMaxArchivOrdersCount(Integer.parseInt(maxArchivOrdersCount.getText()));
        
        settings.save();
    }
    
    private void setContent()
    {
        Settings settings = Settings.getSettings();
        
        preordersUpdatePeriod.setText(Integer.toString(settings.getPreordersUpdatePeriod()));
        maxArchivOrdersCount.setText(Integer.toString(settings.getMaxArchivOrdersCount()));
    }
}
